//
//  ViolationInfoViewController.m
//  carcareIOS
//
//  Created by ileo on 16/8/2.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "ViolationInfoViewController.h"
#import "ViolationInfoCarHeadView.h"
#import "ViolationInfoCell.h"
#import "NodataView.h"
#import "ViolationMapViewController.h"
#import "BRCarInfoViewController.h"
#import "NetViolations.h"

@interface ViolationInfoViewController ()

@property (nonatomic, strong) ViolationInfoCarHeadView *infoView;

@end

@implementation ViolationInfoViewController

-(void)viewDidLoad{
    [super viewDidLoad];
    self.title = @"违章信息";
    
     __weak __typeof(self) wself = self;
    [self navSetupRightImageName:@"editVioCarBtnImage" action:^{
        BRCarInfoViewController * vc = [[BRCarInfoViewController alloc] initWithInputType:TYPE_INPUT_TABLEVIEW vioCarModel:wself.vioCarModel];
        [wself.navigationController pushViewController:vc animated:YES];
    }];
    
    [self.view addSubview:self.infoView];
    
    self.tableView.top = [ViolationInfoCarHeadView height] + 12;
    self.tableView.height -= ([ViolationInfoCarHeadView height] + 12);
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}

-(void)setVioCarModel:(VioCarModel *)vioCarModel{
    _vioCarModel = vioCarModel;
    [self.tableView reloadData];
    self.infoView.vioCarModel = vioCarModel;
}

-(ViolationInfoCarHeadView *)infoView{
    if (!_infoView) {
        _infoView = [[ViolationInfoCarHeadView alloc] initWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, [ViolationInfoCarHeadView height])];
    }
    return _infoView;
}

#pragma mark - tableview delegate

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return (self.vioCarModel.vioModels.count > 0 ? self.vioCarModel.vioModels.count : 1);
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.vioCarModel.vioModels > 0) {
        return [ViolationInfoCell height];
    }else{
        return tableView.height;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger type = self.vioCarModel.vioModels.count > 0 ? 1 : 2;
    NSString *cellID = [NSString stringWithFormat:@"cell%zd",type];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    switch (type) {
        case 1:
        {
            if (!cell) {
                cell = [[ViolationInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
            }
            ((ViolationInfoCell *)cell).vioModel = self.vioCarModel.vioModels[indexPath.row];
        }
            break;
        case 2:
        {
            if (!cell) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
                NodataView *nodata = [[NodataView alloc] initWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, __MAIN_HEIGHT_64 - [ViolationInfoCarHeadView height])];
                nodata.backgroundColor = COLOR_HEXSTRING(@"#f8f8f8");
                nodata.tag = 17;
                [cell addSubview:nodata];
            }
            NodataView *nodata = [cell viewWithTag:17];
            
            [nodata setImage:ImageNamed(@"v_tip_good") attributedString:[self attWithTips:@[@"恭喜你!",@"目前没有查到违章"]]];
//            [nodata setImage:ImageNamed(@"v_tip_fail") attributedString:[self attWithTips:@[@"查询失败"]]];
//            [nodata setImage:ImageNamed(@"v_tip_loading") attributedString:[self attWithTips:@[@"加载中..."]]];
            
        }
            break;
        default:
            break;
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.vioCarModel.vioModels.count > 0) {
        ViolationMapViewController *vc = [[[ViolationMapViewController alloc] init] navSetupBackItemWithIdentification:BACK_ICON_TITLE];
        [vc setVioCarModel:self.vioCarModel index:indexPath.row];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

-(void)loadMoreTopFinish:(void (^)(CGFloat))finish withScrollView:(UIScrollView *)scrollView{
    [self updateFinish:^{
        finish(0);
    } fail:^{
        finish(0);
    }];
}

-(void)viewWillAppear:(BOOL)animated{
    [self updateFinish:nil fail:nil];
}

-(void)updateFinish:(void (^)())finish fail:(void (^)())fail{
    __weak __typeof(self) wself = self;
    [NetViolations vioCarWithCarID:self.vioCarModel.carinfoModel.id unDel:self.vioCarModel.carinfoModel.unDel success:^(VioCarModel *vioCarModel) {
        wself.vioCarModel = vioCarModel;
        if (finish) {
            finish();
        }
    } fail:^{
        if (fail) {
            fail();
        }
    }];
}

#pragma mark - tip
-(NSAttributedString *)attWithTips:(NSArray *)tips{
    NSMutableArray *arr = [NSMutableArray arrayWithCapacity:3];
    for (int i = 0; i < tips.count; i++) {
        if (i != 0) {
            [arr addObject:[NSAttributedString attributedStringWithLineFeedSize:1]];
        }
        [arr addObject:[NSAttributedString attributedStringWithText:tips[i] color:COLOR_HEXSTRING(@"#a4a4a4") font:FONT_DEFAULT_Light(15)]];
    }
    return [NSAttributedString attributedStringWithAttributedStrings:arr];
}

@end
